package com.xlh.dto.courseTest;

import com.xlh.enums.courseTest.QuestionTypeEnum;
import com.xlh.pojo.courseTest.CourseQuestionRepository;
import com.xlh.pojo.courseTest.CourseQuestionRepositoryDetails;
import com.xlh.pojo.courseTest.CourseTestPaperAnswer;
import com.xlh.pojo.courseTest.CourseTestPaperQuestion;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhr 2019/1/21
 */
@Data
@AllArgsConstructor
public class QuestionDTO {
    // 题目id
    private Long questionId;
    // 题号
    private Integer orderNumber;
    // 题干描述
    private String content;
    // 选择题选项
    private List<String> choiceList;
    // 是否多选题
    private Boolean isMultipleChoice;

    /**
     * @param question   题目
     * @param type       题型
     * @param choiceList 选择题选项列表
     */
    public QuestionDTO(
            CourseTestPaperQuestion question, Integer type, List<CourseTestPaperAnswer> choiceList) {
        this(question.getId(), question.getOrderNumber(), question.getHtmlContent(), null, null);
        if (QuestionTypeEnum.isChoice(type)) {
            this.choiceList = choiceList.stream().map(CourseTestPaperAnswer::getContent).collect(Collectors.toList());
            this.isMultipleChoice = choiceList.stream().filter(CourseTestPaperAnswer::getIsCorrect).count() > 1;
        }
    }

    /**
     * @param question    题目
     * @param orderNumber 题号
     * @param type        题型
     * @param choiceList  选择题选项列表
     */
    public QuestionDTO(
            CourseQuestionRepository question, int orderNumber, Integer type,
            List<CourseQuestionRepositoryDetails> choiceList) {
        this(question.getId(), orderNumber, question.getHtmlContent(), null, null);
        if (QuestionTypeEnum.isChoice(type)) {
            this.choiceList = choiceList.stream().map(CourseQuestionRepositoryDetails::getContent).collect(Collectors.toList());
            this.isMultipleChoice = choiceList.stream().filter(CourseQuestionRepositoryDetails::getIsCorrect).count() > 1;
        }
    }
}
